//Originally by Jon Watte.
//Released into the JigLibX project under the JigLibX license.
//Separately released into the public domain by the author.

#region Using Statements
using System.Collections.Generic;
using JigLibX.Geometry;
using JigLibX.Physics;
using Microsoft.Xna.Framework;
using Utilities;
#endregion

namespace JigLibX.Collision
{

    /// <summary>
    /// Implementing a collision system (broad-phase test) based on the sweep-and-prune 
    /// algorithm
    /// </summary>
    public class CollisionSystemSAP : CollisionSystem, IComparer<CollisionSkin>
    {
        List<CollisionSkin> skins_ = new List<CollisionSkin>(15);
        bool dirty_;
        float largestX_;
        ArrayList<CollisionSkin> active_ = new ArrayList<CollisionSkin>(15);
        ArrayList<Sphere> testing_ = new ArrayList<Sphere>(15);
        ArrayList<Sphere> second_ = new ArrayList<Sphere>(15);

        public float LargestX { get { return largestX_; } }

        public CollisionSystemSAP()
        {
        }

        public override void AddCollisionSkin(CollisionSkin collisionSkin)
        {
            //collisionSkin.CollisionSystem = this;
            //skins_.Add(collisionSkin);
            //dirty_ = true;
            //float dx = collisionSkin.WorldBoundingBox.Max.X - collisionSkin.WorldBoundingBox.Min.X;
            //if (dx > largestX_)
            //    largestX_ = dx;
        }

        public override bool RemoveCollisionSkin(CollisionSkin collisionSkin)
        {
            //int ix = skins_.IndexOf(collisionSkin);
            //if (ix >= skins_.Count || ix < 0)
            //    return false;
            //skins_.RemoveAt(ix);
            return true;
        }

        public override List<CollisionSkin> CollisionSkins
        {
            get { return skins_; }
        }

        public override void CollisionSkinMoved(CollisionSkin skin)
        {
            dirty_ = true;
        }

        public override void DetectCollisions(JigLibX.Physics.Body body, BasicCollisionFunctor collisionFunctor, CollisionSkinPredicate2 collisionPredicate, float collTolerance)
        {
            return;
        }

        public override void DetectAllCollisions(ArrayList<JigLibX.Physics.Body> bodies, BasicCollisionFunctor collisionFunctor, CollisionSkinPredicate2 collisionPredicate, float collTolerance)
        {
        }

        public override bool SegmentIntersect(out float fracOut, out CollisionSkin skinOut, out Microsoft.Xna.Framework.Vector3 posOut, 
            out Microsoft.Xna.Framework.Vector3 normalOut, JigLibX.Geometry.Segment seg, CollisionSkinPredicate1 collisionPredicate)
        {

            fracOut = 0.0f;
            skinOut = null;
            posOut = Vector3.Zero;
            normalOut = Vector3.Zero;
            return false;
        }

        public int Compare(CollisionSkin x, CollisionSkin y)
        {
            return 0;
        }
    }
}